package gx.quartzmanager

import spray.servlet.{ServletRequestInfoHeader, WebBoot}
import akka.actor.{Actor, Props, ActorSystem}
import spray.routing._
import spray.http._
import MediaTypes._


class Boot extends WebBoot {

  class RootServiceActor extends Actor with HttpService {
    def actorRefFactory = context

    def receive = runRoute {
      path("quartz") {
        get {
          respondWithMediaType(`text/html`) {
            ctx =>

            // XML is marshalled to `text/xml` by default, so we simply override here
              ctx.complete {
                val head = ctx.request.headers.find(p => p.is("servlet-request-info")).get
                val session = head.asInstanceOf[ServletRequestInfoHeader].hsRequest.getSession(true)
                println("session id = " + session.getId)
                <html>
                  <body>
                    <h1>Say hello to
                      <i>spray-routing</i>
                      on
                      <i>Jetty</i>
                      !</h1>
                    <p></p>
                  </body>
                </html>
              }
          }
        }
      }
    }
  }

  // we need an ActorSystem to host our application in
  val system = ActorSystem("quartzmanager")
  val serviceActor = system.actorOf(Props(new RootServiceActor))

  system.registerOnTermination {
    system.shutdown
  }


}
